<template>
  <div>
    <el-form size="mini"
             inline>
      <el-form-item>
        <el-input v-model="companyName"
                  placeholder="公司名称"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="projectName"
                  placeholder="项目名称"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="username"
                  placeholder="姓名"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button-group>
          <el-button type="primary"
                     icon="el-icon-search"
                     @click="query">查询</el-button>
          <el-button type="success"
                     @click="addGoods">新增</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
    <Ca-rule-table :DataList="goodsList"
                   :header="header"
                   :headle="headle"
                   style="width:90%"
                   :columnIndex="8"
                   field="own_purchase_status"
                   :progressstatus="0"
                   @delete="delitem"
                   @checkleave="checkitem"></Ca-rule-table>
    <paging :currentpage="currentpage"
            :currentlimit="currentlimit"
            :total="10"
            @setpage="getpage"
            @setlimit="getlimit"></paging>
    <el-dialog title="物品采购"
               :visible.sync="isopen"
               top="8vh"
               v-dialogDrag>
      <add-goods v-if="isopen"
                 :entryList="entryList"
                 :ownHead="ownHead"
                 :hisComment="hisComment"
                 :openType="openType"
                 @close="closewin"></add-goods>
    </el-dialog>
  </div>
</template>

<script>
import addGoods from "@/components/Ca-to-do/add-goods";
import { changetime } from "@/components/global-fn/global-fn";
import {
  apigetOwnHeadList,
  apiOwnHeadListById,
  apidelOwnEntry
} from "@/request/api.js";
export default {
  name: "GoodsPurchase",
  data () {
    return {
      companyName: "",
      projectName: "",
      username: "",
      currentlimit: 15,
      currentpage: 1,
      isopen: false,
      header: [
        ["单据编号", "own_purchase_id", 100],
        ["公司名称", "company_name", 100],
        ["项目名称", "construct_project_name", 130],
        ["建单时间", "own_purchase_time", 100],
        ["计划时间", "own_purchase_planDate", 100],
        ["希望送达时间", "own_purchase_arriveDate", 120],
        ["计划员", "own_purchase_planMan", 90],
        ["采购类型", "own_purchase_type", 100],
        ["状态", "own_purchase_status2", 100]
      ],
      headle: ["查看", "删除"],
      goodsList: [],
      entryList: [],
      ownHead: {},
      hisComment: [],
      openType: ""
    };
  },
  created () {
    this.getGoodsList();
  },
  components: {
    addGoods,
  },
  methods: {
    query () {
      this.currentlimit = 15;
      this.currentpage = 1;
      this.getGoodsList();
    },
    //关闭弹窗
    closewin () {
      this.isopen = false;
      this.getGoodsList();
    },
    delitem (row) {
      console.log(row);
      this.$confirm(`确认删除吗？`)
        .then(() => {
          apidelOwnEntry({
            own_purchase_entryId: row.own_purchase_id
          }).then(res => {
            console.log(res);
            this.getGoodsList();
          });
        })
        .catch(() => { });
    },
    //查看
    checkitem (row) {
      let data = {
        own_purchase_id: row.own_purchase_id
      };
      apiOwnHeadListById(data).then(res => {
        console.log(res);
        this.ownHead = res.data.ownHead;
        this.entryList = res.data.entryList;
        this.hisComment = res.hisComment.map(item => {
          item.END_TIME_ = item.END_TIME_ ? changetime(item.END_TIME_) : "";
          return item;
        });
        this.isopen = true;
        this.openType = "check";
      });
    },
    getpage (val) {
      this.currentpage = val;
      this.getGoodsList();
    },
    getlimit (val) {
      this.currentlimit = val;
      this.getGoodsList();
    },
    addGoods () {
      this.openType = "add";
      this.entryList = [];
      this.hisComment = [];
      this.ownHead = {
        own_purchase_companyId: "", //(必填)公司编号
        own_purchase_projectId: "", //必填)项目编号
        own_purchase_planDate: "", //必填)计划日期
        own_purchase_arriveDate: "", //必填)希望送达日期
        own_purchase_planMan: sessionStorage.getItem("username"), //必填)复核员
        own_purchase_brand: "", //必填)品牌
        own_purchase_type: 1, //必填)采购类型
        userid: 0 //必填)下一审核人id
      };
      this.isopen = true;
    },
    getGoodsList () {
      let data = {
        company_name: this.companyName,
        construct_project_name: this.projectName,
        own_purchase_planMan: this.username,
        pageSize: this.currentlimit,
        limit: this.currentpage
      };
      apigetOwnHeadList(data).then(res => {
        console.log(res);
        this.goodsList = res.data.map(item => {
          item.own_purchase_type =
            item.own_purchase_type == 1 ? "普通采购" : "材料采购";
          item.own_purchase_status2 =
            item.own_purchase_status == 0
              ? "审核中"
              : item.own_purchase_status == 1
                ? "审核通过"
                : item.own_purchase_status == 2
                  ? "审核不通过"
                  : "被驳回";
          return item;
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
